import models.Album;
import models.Photo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import service.AlbumManager;

import java.util.Iterator;
import java.util.List;

/**
 * Created by VLAD on 19.08.2014.
 */
public class Main {
    public static void main(String[] args)
    {

        ApplicationContext ac = new FileSystemXmlApplicationContext("ApplicationContext.xml");

        AlbumManager am = ac.getBean(AlbumManager.class);

        //Saving six photos to imaging DB
        Photo p1 = am.savePhoto("photo1");
        Photo p2 = am.savePhoto("photo2");
        Photo p3 = am.savePhoto("photo3");
        Photo p4 = am.savePhoto("photo4");
        Photo p5 = am.savePhoto("photo5");
        Photo p6 = am.savePhoto("photo6");

        //Creating three albums in DB
        Album a1 = am.createAlbum("alb1", "photos 1 3 6");
        Album a2 = am.createAlbum("alb2", "photos 2 4  ");
        Album a3 = am.createAlbum("alb3", "photos 5    ");
        Album a4 = am.createAlbum("alb4", "photos      ");

        //Adding some photos to some albums. The fourth album is empty,
        // it would be deleted automatically from DB
        am.addPhotoToAlbum(a1, p1);
        am.addPhotoToAlbum(a1, p3);
        am.addPhotoToAlbum(a1, p6);

        am.addPhotoToAlbum(a2, p2);
        am.addPhotoToAlbum(a2, p4);

        am.addPhotoToAlbum(a3, p5);

        //Showing our new albums with it's photos
        List l = am.getAllAlbums();
        showAlbums(l);

        //Deleting some photos from album one and three
        //Third album has one photo. After deleting this photo
        //album is empty. It shows deleting empty album automatically.
        am.delPhotoFromAlbum(a1, p3);
        am.delPhotoFromAlbum(a1, p1);
        am.delPhotoFromAlbum(a3, p5);

        //Showing our new albums with it's photos after deleting
        l = am.getAllAlbums();
        showAlbums(l);


    }

    public static void showAlbums(List la){
        System.out.println("albums: ");
        Iterator<Album> iter1 = la.iterator();

        while (iter1.hasNext())
        {
            System.out.println(iter1.next());
        }
        System.out.println();
    }
}
